
package net.stuffrepos.bricknetcenter.util.gui.inputcomponent;

import javax.swing.JTextField;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public class TextFieldIntegerParser implements InputComponentValueParser<JTextField, Integer> {

    @Override
    public ValueResult<Integer> fromComponent(JTextField component) {
        if (component.getText().trim().isEmpty()) {
            return new ValueResult<Integer>(null, true);
        } else {
            try {
                return new ValueResult<Integer>(Integer.parseInt(component.getText().trim()), true);
            } catch (NumberFormatException ex) {
                return new ValueResult<Integer>(null, false);
            }
        }
    }

    @Override
    public void toComponent(JTextField component, Integer value) {
        component.setText(value == null ? "" : value.toString());
    }

    @Override
    public void setEditable(JTextField component, boolean editable) {
        component.setEditable(editable);
    }
}
